<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends CommonController
{

    public function index(){
        $user = session('USER_AUTH');
        $this->assign('user',$user);
        // p($user);
        // die;
        $news = D('News') -> selectNews(array('state'=>'0'));
        $this->assign('news' , $news[0]);
        $Model = new \Think\Model();
    	if($user['roleid']==2||$user['roleid']==6||$user['roleid']==11){
            $document=$Model->query("select count(doc_id) as shuliang from pt_document where doc_id in (select pt_document.doc_id from pt_document,pt_audit,pt_account,pt_mortgage,pt_reason where pt_document.sign_id={$user['userid']} and pt_document.ignore_sign='n' and (pt_account.state=0 or pt_audit.state=0 or pt_mortgage.real_time='') and pt_document.isdel=0   and pt_document.doc_id=pt_account.doc_id and pt_document.doc_id=pt_audit.doc_id and pt_document.doc_id=pt_mortgage.doc_id and pt_document.doc_id=pt_reason.doc_id)");
        }
        elseif($user['roleid']==1){
            $document=$Model->query("select count(doc_id) as shuliang from pt_document where doc_id in (select pt_document.doc_id from pt_document,pt_audit,pt_account,pt_mortgage,pt_reason where pt_document.market_id={$user['userid']} and pt_document.ignore_market='n' and (pt_account.state=0 or pt_audit.state=0 or pt_mortgage.real_time='') and pt_document.isdel=0   and pt_document.doc_id=pt_account.doc_id and pt_document.doc_id=pt_audit.doc_id and pt_document.doc_id=pt_mortgage.doc_id and pt_document.doc_id=pt_reason.doc_id)");
        }
        $count = M('document')->where(array('isdel'=>'0','state'=>'2'))->count();
        $this->assign('count',$count);
      	$this->assign('document',$document);
        //p($_SESSION);die;
        $this->display('Index/index');
    }



    public function creatmortgage(){
        // if(I('post.token')===session('token')){
        //     $this->jsonReply('200' , '请不要重复提交');
        // }
        // else{
            if(I('post.buy_name')!="" && I('post.buy_card')!="" && I('post.bank_id') && I('post.total_amount') && I('post.market_id') && I('post.area')){
        		$user_auth = session('USER_AUTH');//输出session值
        	    $Document = M("document"); // 档案表添加
                $data['buy_name'] =  I('post.buy_name'); //I('post.')
                $data['sell_name'] = I('post.sell_name') ;
                $data['buy_card'] = I('post.buy_card') ;
                $data['spouse_card'] = I('post.spouse_card') ;
                $data['address'] = I('post.address') ;
                $data['supervise'] = I('post.supervise') ;
                $data['agent_id'] = I('post.agent') ;
                $data['bank_id'] = I('post.bank_id') ;
                $data['create_time'] = strtotime(I('post.create_time')) ;
                $data['sign_id'] = I('post.sign_id') ;
                $data['market_id'] = I('post.market_id') ;
                $data['amount'] = I('post.total_amount') ;
                $data['place'] = $user_auth['place'] ;
                $data['area'] = I('post.area');
                $data['doc_type'] =I('post.doc_type');
                $data['need_day'] =I('post.needday');
                $doc_id=$Document->add($data);
                $Audit = M("audit"); // 银行审核表添加
                $dataaudit['doc_id']=$doc_id;
                $audit=$Audit->add($dataaudit);

                $Account = M("account"); // 公积金核表添加
                $dataaccount['doc_id']=$doc_id;
                $account=$Account->add($dataaccount);

                $Mortgage = M("mortgage"); // 抵押表添加
                $datamortgage['doc_id']=$doc_id;
                $mortgage=$Mortgage->add($datamortgage);

                $Extend = M("extend"); // 信息扩展表添加
                $dataextend['doc_id'] = $doc_id;
                $dataextend['nature'] = I('post.nature');
                $dataextend['buy_phone'] = I('post.buy_phone');
                $dataextend['buy_tel'] = I('post.buy_tel');
                $dataextend['buy_work'] = I('post.buy_work');
                $dataextend['buy_work_tel'] = I('post.buy_work_tel');
                $dataextend['buy_spouse_name'] = I('post.buy_spouse_name');
                $dataextend['buy_spouse_tel'] = I('post.buy_spouse_tel');
                $dataextend['sell_phone']=I('post.sell_phone');
                $dataextend['sell_tel']=I('post.sell_tel');
                $dataextend['account_amount']=I('post.account_amount');
                $dataextend['account_type']=I('post.account_type');
                $dataextend['business_amount']=I('post.business_amount');
                $dataextend['total_amount']=I('post.total_amount');
                $dataextend['loan_year']=I('post.loan_year');
                $dataextend['loan_remark']=I('post.loan_remark');
                $dataextend['house_area']=I('post.house_area');
                $dataextend['house_age']=I('post.house_age');
                $dataextend['house_total']=I('post.house_total');
                $dataextend['agent_name']=I('post.agent_name');
                $dataextend['agent_tel']=I('post.agent_tel');
                $dataextend['agent_address'] = I('post.agentAddress');
                $extend=$Extend->add($dataextend);

                $Charge = M("charge"); // 收费发证表添加
                $datacharge['doc_id']=$doc_id;
                $charge=$Charge->add($datacharge);

                if($charge && $mortgage && $account && $audit && $doc_id && $extend){
                    $this -> jsonReply('200' , '建档成功');
                  }
                  else{
                    $this -> jsonReply('500' , '建档失败');
                  }
        	}
        	else{
        		$this -> jsonReply('500' , '请填写买方姓名、买方身份证、贷款银行以及贷款金额');
        	}
        // }


    }



    public function mortgagelist(){
    	$user_auth = session('USER_AUTH');  //输出session值
    	$this->assign('user_auth' , $user_auth);
    	$Document = M("document"); // 档案表查询

    	$xiaji = M('user')->where(array('leader'=>$user_auth['userid']))->select();

    	foreach($xiaji as $k=>$v){
    	    $ids[] = $v['user_id'];
    	    $xiaxiaji = M('user')->where(array('leader'=>$v['user_id']))->select();
            foreach($xiaxiaji as $ka=>$va){
                $ids[]=$va['user_id'];
            }
    	}
    	//p($ids);
    	if($user_auth['roleid']==1){

    	    if(is_array($ids)){
    	        $data['market_id']=array( array('eq',$user_auth['userid']) , array('in',$ids) , 'or');
    	    }
          else{
            $data['market_id']=array('eq',$user_auth['userid']) ;
          }
    		//$data['market_id']=array( array('eq',$user_auth['userid']) , array('in',$ids) , 'or');
    	}
    	elseif($user_auth['roleid']==2 ){
    	    if(is_array($ids)){
    	        $data['sign_id']= array( array('eq',$user_auth['userid']) , array('in',$ids) , 'or');
    	    }
          else{
            $data['market_id|sign_id']=array('eq',$user_auth['userid']) ;
          }
    	}
      elseif($user_auth['roleid']==6){
        $data['sign_id']=array('eq',$user_auth['userid']) ;
        if(count($ids)>0){
            $data['sign_id']= array( array('eq',$user_auth['userid']) , array('in',$ids) , 'or');
        }
      }
    	elseif($user_auth['roleid']==3 ){
    	    $data['place'] = $user_auth['place'];
    	    if(!empty($user_auth['banks'])){
    	        $data['bank_id'] = array('IN',$user_auth['banks']);
    	    }
    	}
    	elseif( $user_auth['roleid']==11 ){
    		//$data['place']= '3';

    		$data['_string'] = "place=3 OR turncity=1 ";
    	}
      elseif($user_auth['roleid']==13){//江宁负责人
    	    $data['place|turncity']= '2';
    	}
    	elseif($user_auth['roleid']==14){//江北负责人
    	    $data['_string'] = "place=3 OR turncity=1 OR market_id=".$user_auth['userid'];
    	}
    	elseif($user_auth['roleid']==15){
    	    $data['place']= '1';
    	}
    	elseif($user_auth['roleid']==16){
    	    $data['area']= $user_auth['place'];
    	}

      elseif($user_auth['roleid']==17){
    	    $data['place']= $user_auth['place'];
          if(strpos($user_auth['banks'],',')){
            $bank = explode(',' , $user_auth['banks']);
            $data['bank_id'] = array('in' , $bank);
          }
          else{
            $data['bank_id'] = $user_auth['banks'];
          }
          // p($data);
    	}

    	if(I('param.content')!=''){
    	    if(I('param.type')==8){
    	    	$data['buy_name']=array('LIKE','%'.I('param.content').'%');
    	    }
    	    elseif(I('param.type')==1){
    	    	$data['sell_name']=array('LIKE','%'.I('param.content').'%');
    	    }
    	    elseif(I('param.type')==2){
    	    	$data['buy_card'] = array('LIKE','%'.I('param.content').'%');
    	    }
    	    elseif(I('param.type')==3){
    	    	$data['address']=array('LIKE','%'.I('param.content').'%');
    	    }
    	    elseif(I('param.type')==4){
    	    	$data['agent_id']=array('LIKE','%'.I('param.content').'%');
    	    }
    	    elseif(I('param.type')==5){
    	    	$signname = M('user')->where('user_name="'.I('param.content').'"')->find();//签单人姓名
                $data['sign_id']=$signname['user_id'];
    	    }
    	    elseif(I('param.type')==6){
    	    	$marketname = M('user')->where('user_name="'.I('param.content').'"')->find();//营销人姓名
                if($marketname){$data['market_id']=$marketname['user_id'];}
    	    }
          elseif(I('param.type')==7){
              $bankname = M('bank')->where( array( 'isdel'=>'0' , 'bank_name'=>array('like','%'.I('param.content').'%') ) )->find();//银行总行名称
              if($bankname){
                  $data['bank_id'] = $bankname['bank_id'];
              }
          }
          elseif(I('param.type')==9){
              $data['loan']=array('LIKE','%'.I('param.content').'%');
          }
    	}
    	$data['isdel']='0';
    //p($data);

      $p = I('get.p');
      $p = empty($p) ? 1 : $p;
    	$count = $Document->where($data)->count();
    	$n=15;
        $page=new \Think\Page($count,$n);
        foreach($map as $key=>$val) {
            $Page->parameter[$key]   =   urlencode($val);
        }
        $show= $page->show();// 分页显示输出
        $this->assign('page',$show);
        $this->assign('p',$p);
        $this->assign('type',I('param.type'));
      	$this->assign('content',I('param.content'));
    	$document=$Document->where($data)->page($p.','.$n)->order('doc_id desc')->select();
        foreach ($document as $key=>$vo)
        {
            $User = M("user");
            $user=$User->where("user_id=".$vo['sign_id'])->field('user_name')->find();
            $document[$key]['signname']=$user['user_name'];
            $charge=M('charge')->where("doc_id=".$vo['doc_id'])->field('charge_time,certificate_time')->find();
            $document[$key]['charge']=$charge['charge_time'];
            $document[$key]['certificate_time']=$charge['certificate_time'];
        }
    	$this->assign('document',$document);
    	$this->assign('documentcount',$count);
        $this->display('Index/mortgage-list');
    }


    public function mortgageadd(){
        // session('token' , time());
        // $this->assign('token' , session('token'));
    	$user_auth = session('USER_AUTH');//输出session值
        $this->assign('user_auth' , $user_auth);
    	$banks = M('bank') ->where('isdel=0 and pid=0 ')-> select();//银行列表
        $this->assign('banks' , $banks);

        $marketlist = M('user')->where('isdel=0 and level=1 or level=14')->order('user_id asc')->select();//营销人列表
        $this->assign('marketlist' , $marketlist);
        $this->display('Index/mortgage-add');
    }


    public function mortgagealert(){
        // session('token' , time());
        // $this->assign('token' , session('token'));
    	$user_auth = session('USER_AUTH');//输出session值

    	$doc_id=$_GET['doc_id'];
    	$Document = M("document"); // 档案表查询
        $documentlist = $Document->where('doc_id='.$doc_id)->find();
        $this->assign('documentlist',$documentlist);

        $Audit = M("audit"); // 银行审核表查询
        $auditlist = $Audit->where('doc_id='.$doc_id)->find();
        $this->assign('auditlist',$auditlist);

        $Account = M("account"); // 公积金核表查询
        $accountlist = $Account->where('doc_id='.$doc_id)->find();
        $this->assign('accountlist',$accountlist);

        $Mortgage = M("mortgage"); // 抵押表查询
        $mortgagelist = $Mortgage->where('doc_id='.$doc_id)->find();
        $this->assign('mortgagelist',$mortgagelist);

        $Extend = M("extend"); // 信息扩展表查询
        $extendlist = $Extend->where('doc_id='.$doc_id)->find();
        $this->assign('extendlist',$extendlist);

        $Charge = M("charge"); // 收费发证表查询
        $chargelist = $Charge->where('doc_id='.$doc_id)->find();
        $this->assign('chargelist',$chargelist);

        $banks = M('bank') ->where('isdel=0 and pid=0')-> select();//银行总行列表
        $this->assign('banks' , $banks);
        $bankslist = M('bank') ->where('isdel=0 and pid='.$documentlist['bank_id'])-> select();//银行支行列表
        $this->assign('bankslist' , $bankslist);
        $mainbanks = M('bank') ->where('bank_id='.$documentlist['bank_id'])-> find();//贷款银行总行
        $this->assign('mainbanks' , $mainbanks);
        if($auditlist['bank_id']!=''){
        	$partbanks = M('bank') ->where('bank_id='.$auditlist['bank_id'])-> find();//银行支行
            $this->assign('partbanks' , $partbanks);
        }

        $reason = M('reason')->where('doc_id='.$doc_id.' and type=1')->select();//银行审核未通过原因
        $this->assign('reason' , $reason);

        $reasonlist = M('reason')->where('doc_id='.$doc_id.' and type=2')->select();//公积金审核未通过原因
        $this->assign('reasonlist' , $reasonlist);



        $agents = M('Agent')->where('isdel=0')->order('agent_id asc')->select();//中介列表
        $this->assign('agents' , $agents);

        $agentname = M('Agent')->where('agent_id=0')->find();//中介公司名称查询
        $this->assign('agentname' , $agentname);

        $marketlist = M('user')->where('isdel=0 and level=1 or level=14')->order('user_id asc')->select();//营销人列表
        $this->assign('marketlist' , $marketlist);

        $marketname = M('user')->where('user_id='.$documentlist['market_id'])->find();//营销人姓名
        $this->assign('marketname' , $marketname);
        $signname = M('user')->where('user_id='.$documentlist['sign_id'])->find();//签单人姓名
        $this->assign('signname' , $signname);

        $this->assign('user_auth' , $user_auth);
        if(I('get.type')=='1'){
          $this->display('Index/unlock');
        }
        else{$this->display('Index/mortgage-alert');}
    }




    public function alertmortgage(){
        // if(I('post.token')==session('token')){
        //     $this->jsonReply('200' , '请不要重复提交');
        // }

    	$user_auth = session('USER_AUTH');//输出session值
    	$doc_id= I('post.doc_id');
      $state = M('document')->where(array('doc_id'=>$doc_id))->field('state,part_bank_id')->find();
      if(I('post.doc_type')){
        $data['doc_type'] = I('post.doc_type');
      }
      if(I('post.needday')){
        $data['need_day'] = I('post.needday');
      }

    	if( strstr($user_auth['level'],"26") && ($state['state']=='0' || $user_auth['roleid']==4)){//建档权限
    		$data['buy_name'] =  I('post.buy_name');
            $data['sell_name'] = I('post.sell_name') ;
            $data['buy_card'] = I('post.buy_card') ;
            $data['spouse_card'] = I('post.spouse_card') ;
            $data['address'] = I('post.address') ;
            $data['supervise'] = I('post.supervise') ;
            $data['agent_id'] = I('post.agent') ;
            $data['bank_id'] = I('post.bank_id') ;
            $data['area'] = I('post.area');
            if(I('post.create_time')!=""){
                $data['create_time'] = strtotime(I('post.create_time')) ;
            }
            $data['sign_id'] = I('post.sign_id') ;
            $data['market_id'] = I('post.market_id') ;
            $data['amount'] = I('post.total_amount') ;

            $dataextend['agent_address'] = I('post.agentAddress');
            $dataextend['nature']=I('post.nature');
            $dataextend['buy_phone']=I('post.buy_phone');
            $dataextend['buy_tel']=I('post.buy_tel');
            $dataextend['buy_work']=I('post.buy_work');
            $dataextend['buy_work_tel']=I('post.buy_work_tel');
            $dataextend['buy_spouse_name']=I('post.buy_spouse_name');
            $dataextend['buy_spouse_tel']=I('post.buy_spouse_tel');
            $dataextend['sell_phone']=I('post.sell_phone');
            $dataextend['sell_tel']=I('post.sell_tel');
            $dataextend['account_amount']=I('post.account_amount');
            $dataextend['account_type']=I('post.account_type');
            $dataextend['business_amount']=I('post.business_amount');
            $dataextend['total_amount']=I('post.total_amount');
            $dataextend['loan_year']=I('post.loan_year');
            $dataextend['loan_remark']=I('post.loan_remark');
            $dataextend['house_area']=I('post.house_area');
            $dataextend['house_age']=I('post.house_age');
            $dataextend['house_total']=I('post.house_total');
            $dataextend['agent_name']=I('post.agent_name');
            $dataextend['agent_tel']=I('post.agent_tel');

            if(I('post.transfer_real_time')!=""){$dataextend['transfer_real_time']=strtotime(I('post.transfer_real_time'));}
            if(I('post.certificate_time')!=""){$dataextend['certificate_time']=strtotime(I('post.certificate_time'));}
    	}
    	if(strstr($user_auth['level'],"27")){//银行审核
        if(I('post.part_bank_id')){
          $dataaudit['bank_id']=I('post.part_bank_id') ;
          $data['state']=1;
          $data['part_bank_id']=I('post.part_bank_id') ;
        }
        if(I('post.loan')){
          $data['loan'] = I('post.loan') ;
        }

            if(I('post.plan_time')!=""){
              $dataaudit['plan_time']=strtotime(I('post.plan_time'));
            }

            if(I('post.reason')==null){//ttmr改了，判断出错
            	$dataaudit['state']=1;
            }
            else{
                $dataaudit['state']='0';
                $dataaudit['reason']=I('post.reason') ;
                if($user_auth['roleid']==2||$user_auth['roleid']==6||$user_auth['roleid']==11){
                  $data['ignore_sign'] = 'n';
                }
                elseif($user_auth['roleid']==1){
                  $data['ignore_market'] = 'n';
                }

            }
            if(I('post.audit_time')!=""){
              $data['audit_time'] = strtotime(I('post.audit_time')) ;
            }
            if(I('post.audit_finish')!=""){
              $data['audit_finish'] = strtotime(I('post.audit_finish')) ;
            }
            if(I('post.reason')!=null){
            	$auditreason['doc_id'] = $doc_id ;
            	$auditreason['type'] = 1 ;
            	$auditreason['creat_time'] = time() ;
            	$auditreason['content'] = I('post.reason') ;
              $auditreason['bank_id'] = $state['part_bank_id'];
            	$Reason = M("reason"); // 银行审核未通过原因添加
                $Reason->add($auditreason);

            }
    	}

    	if(strstr($user_auth['level'],"28")){//南房

        if(I('post.transfer_real_time')!=""){

          $dataextend['transfer_real_time']=strtotime(I('post.transfer_real_time'));
        }
        if(I('post.certificate_time')!=""){
          $dataextend['certificate_time']=strtotime(I('post.certificate_time'));
        }
    	}


    	if(strstr($user_auth['level'],"29")){//不动产证
    		if(I('post.estate_time')!=""){
            $dataextend['estate_time']=strtotime(I('post.estate_time'));
          }
        if(I('post.commission')){
          $dataextend['commission']=I('post.commission');
        }
        if(I('post.estate_remark')){
          $dataextend['estate_remark']=I('post.estate_remark');
        }
      }

    	if(strstr($user_auth['level'],"30") ){//公积金
    	   // $ba = M('audit')->where(array('doc_id'=>$doc_id))->find();
    	   // if($ba['state']=='1'){
    	        if(I('post.account_audit_time')!=""){$dataaccount['audit_time']=strtotime(I('post.account_audit_time'));}
                if(I('post.account_plan_time')!=""){$dataaccount['plan_time']=strtotime(I('post.account_plan_time'));}
                if(I('post.account_real_time')!=""){$dataaccount['real_time']=strtotime(I('post.account_real_time'));}
                $dataaccount['reason']=I('post.account_reason');
                if(empty(I('post.account_reason'))){
                    $dataaccount['state']='1';
                }
                else{
                    $dataaccount['state']='0';
                    if($user_auth['roleid']==2||$user_auth['roleid']==6||$user_auth['roleid']==11){
                      $data['ignore_sign'] = 'n';
                    }
                    elseif($user_auth['roleid']==1){
                      $data['ignore_market'] = 'n';
                    }
                }
                //$dataaccount['notice']=I('post.notice');//是否通知客户,表单中没有提交
                if(I('post.account_reason')!=null){
                	$auditreason['doc_id'] = $doc_id ;
                	$auditreason['type'] = 2 ;
                	$auditreason['creat_time'] = time() ;
                	$auditreason['content'] = I('post.account_reason') ;
                	$Reason = M("reason"); // 公积金审核未通过原因添加
                    $Reason->add($auditreason);
                }
    	   // }

    	}

        if(strstr($user_auth['level'],"45")){
            if(I('post.transfer_time')!=""){
              $dataextend['transfer_time']=strtotime(I('post.transfer_time'));
            }
            if(I('post.mortgage_transfer_time')!=""){
    	        $datamortgage['transfer_time']=strtotime(I('post.mortgage_transfer_time'));
            }
        }

    	if(strstr($user_auth['level'],"31")){//抵押
    	    if(I('post.mortgage_transfer_time')!=""){
    	        $datamortgage['transfer_time']=strtotime(I('post.mortgage_transfer_time'));}
            if(I('post.mortgage_get_time')!=""){
                $datamortgage['get_time']=strtotime(I('post.mortgage_get_time'));
            }
            if(I('post.mortgage_reason')){
              $datamortgage['reason']=I('post.mortgage_reason');
              $auditreason['doc_id'] = $doc_id ;
              $auditreason['type'] = 3 ;
              $auditreason['creat_time'] = time() ;
              $auditreason['content'] = I('post.mortgage_reason') ;
              $Reason = M("reason"); // 公积金审核未通过原因添加
                $Reason->add($auditreason);
                if($user_auth['roleid']==2||$user_auth['roleid']==6||$user_auth['roleid']==11){
                  $data['ignore_sign'] = 'n';
                }
                elseif($user_auth['roleid']==1){
                  $data['ignore_market'] = 'n';
                }
            }
            else{
              $datamortgage['reason']='';
            }

            if(I('post.mortgage_plan_time')!=""){
                $datamortgage['plan_time']=strtotime(I('post.mortgage_plan_time'));
            }
            if(I('post.mortgage_real_time')!=""){
                $datamortgage['real_time']=strtotime(I('post.mortgage_real_time'));
                $datamortgage['state']=1;
            }
    	}

    	if(strstr($user_auth['level'],"32")){//他证
    		if(I('post.else_time')!=""){$dataextend['else_time']=strtotime(I('post.else_time'));}
            if(I('post.else_plan_time')!=""){$dataextend['else_plan_time']=strtotime(I('post.else_plan_time'));}
    	}
    	if(strstr($user_auth['level'],"33")){//放款
    		if(I('post.credit_real_time')!=""){
          $data['credit_real_time'] = strtotime(I('post.credit_real_time')) ;
        }
    		if(I('post.credit_time')!=""){
          $data['credit_time']=strtotime(I('post.credit_time'));$dataextend['credit_time']=strtotime(I('post.credit_time'));
        }
        if(I('post.credit_real_money')){
          $dataextend['credit_real_money']=I('post.credit_real_money');
        }
        if(I('post.credit_remark')){
          $dataextend['credit_remark']=I('post.credit_remark');
        }


    	}
    	if(strstr($user_auth['level'],"34")){//收费
    		if(I('post.charge_time')!=""){
          $datacharge['charge_time']=I('post.charge_time');
        }
    	}
    	if(strstr($user_auth['level'],"35")){//发证
        if(I('post.certificate2_time')!=''){
          $datacharge['certificate_time']=strtotime(I('post.certificate2_time'));
        	$datacharge['certificate']=1;
          $datacharge['put_userid']=$_SESSION['userid'];
        }
        $data['latent'] = I('post.latent') ;
    	}

        $data['approver'] = $user_auth['userid'] ;
        $Document = M("document"); // 档案表更新
        $document=$Document->where('doc_id='.$doc_id)->save($data);

        $Audit = M("audit"); // 银行审核表更新
        $audit=$Audit->where('doc_id='.$doc_id)->save($dataaudit);

        $Account = M("account"); // 公积金核表更新
        $account=$Account->where('doc_id='.$doc_id)->save($dataaccount);

        $Mortgage = M("mortgage"); // 抵押表更新
        $mortgage = $Mortgage->where('doc_id='.$doc_id)->save($datamortgage);

        $Extend = M("extend"); // 信息扩展表更新
        $extend=$Extend->where('doc_id='.$doc_id)->save($dataextend);

        $Charge = M("charge"); // 收费发证表更新
        $charge=$Charge->where('doc_id='.$doc_id)->save($datacharge);

        if($charge || $mortgage || $account || $audit || $document || $extend){
            if($user_auth['roleid']=='3' || $user_auth['roleid']=='7' || $user_auth['roleid']=='11'){
                $this -> jsonReply('200' , '审核成功');
            }
            else{
                $this -> jsonReply('200' , '修改成功');
            }
          }
        else{
            if($user_auth['roleid']=='3' || $user_auth['roleid']=='7' || $user_auth['roleid']=='11'){
                $this -> jsonReply('500' , '审核失败');
            }
            else{
                $this -> jsonReply('500' , '修改失败');
            }
        }

    }


    public function remind(){
    	$user = session('USER_AUTH');
    	$Model = new \Think\Model();
      if($user['roleid']==2||$user['roleid']==6||$user['roleid']==11){
            $document=$Model->query("select * from pt_document where doc_id in (select pt_document.doc_id from pt_document,pt_audit,pt_account,pt_mortgage,pt_reason where pt_document.sign_id={$user['userid']} and pt_document.ignore_sign='n' and (pt_account.state=0 or pt_audit.state=0 or pt_mortgage.real_time='') and pt_document.isdel=0   and pt_document.doc_id=pt_account.doc_id and pt_document.doc_id=pt_audit.doc_id and pt_document.doc_id=pt_mortgage.doc_id and pt_document.doc_id=pt_reason.doc_id)");
        }
      elseif($user['roleid']==1){
          $document=$Model->query("select * from pt_document where doc_id in (select pt_document.doc_id from pt_document,pt_audit,pt_account,pt_mortgage,pt_reason where pt_document.market_id={$user['userid']} and pt_document.ignore_market='n' and (pt_account.state=0 or pt_audit.state=0 or pt_mortgage.real_time='') and pt_document.isdel=0   and pt_document.doc_id=pt_account.doc_id and pt_document.doc_id=pt_audit.doc_id and pt_document.doc_id=pt_mortgage.doc_id and pt_document.doc_id=pt_reason.doc_id)");
      }
    	//$document=$Model->query("select * from pt_document where pt_document.doc_id in (select pt_document.doc_id from pt_document,pt_audit,pt_account,pt_mortgage,pt_reason where ((pt_document.sign_id={$user['userid']} and pt_document.ignore_sign='n') or (pt_document.market_id={$user['userid']} and pt_document.ignore_merket='n')) and (pt_account.state=0 or pt_audit.state=0 or pt_mortgage.real_time='') and pt_document.isdel=0  and pt_document.doc_id=pt_account.doc_id and pt_document.doc_id=pt_audit.doc_id and pt_document.doc_id=pt_mortgage.doc_id and pt_document.doc_id=pt_reason.doc_id)");
    	foreach ($document as $key=>$vo)
        {
            $Reason = M("reason");
            $reason=$Reason->where("doc_id=".$vo['doc_id'])->field('content,type')->order('reason_id desc')->limit(1)->select();
            $document[$key]['reason']=$reason[0]['content'];
            $document[$key]['reasontype']=$reason[0]['type'];
            if($user['roleid']==2||$user['roleid']==6||$user['roleid']==11){
              $save = M('document')->where(array('doc_id'=>$vo['doc_id']))->save(array('ignore_sign'=>'y'));
            }
            elseif($user['roleid']==1){
              $save = M('document')->where(array('doc_id'=>$vo['doc_id']))->save(array('ignore_market'=>'y'));
            }

        }
    	$this->assign('document',$document);
      $this->display('Index/remind');
    }


    public function sendmessage(){
      $type=I('post.type');
      $phone=I('post.phone');
      $username=I('post.username');
      $doc_id=I('post.doc_id');
      $address = I('post.address');
      if($address=='1'){
          $addre = '大厂杨新路5号鑫庭雅苑17幢108门面房';
          $tel = '57797986';
      }
      else{
          $addre = '浦口区文昌路鑫海新寓15号门面房';
          $tel = '58884986';
      }
      $doc = M('document') -> where(array('doc_id'=>$doc_id))->find();
      if($type==1){
      	$content="南京平涛管理有限公司提醒您，".$username."客户的案子，银行审核已通过";
      }
      elseif($type==2){
      	$content="南京平涛管理有限公司提醒您，".$username."客户的案子，公积金审核已通过";
      }
      elseif($type==3){
      	$content="南京平涛管理有限公司提醒您，".$username."客户的案子，抵押审核已通过";
      }
      elseif($type==4){
      	$content="南京平涛管理有限公司提醒您，".$username."客户的案子，已放款";
      }
      elseif($type==5){
          $phone=I('post.phone1');
          if($doc['turncity']=='0'){
            if($doc['place']=='1'){
                $content=$username."客户您好，南京平涛管理有限公司提醒您，您的银行贷款申请已结束，请产权人本人接到短信一周以后携带本人身份证原件到鼓楼区管家桥51号建设银行303室领取房产证及土地证。电话:84700377,联系人:马丽丽。";
            }
            elseif($doc['place']=='2'){
                $content=$username."客户您好，南京平涛管理有限公司提醒您，您的银行贷款申请已结束，请您携带本人身份证原件到江宁区新亭西路208号平涛投资领取房产证及土地证。电话:51198340联系人:储青霞。";
            }
            else{
                $content=$username."客户您好，南京平涛管理有限公司提醒您，您的银行贷款申请已结束，请您携带本人身份证原件到".$addre."平涛投资领取房产证及土地证。电话:".$tel;
            }
          }
          elseif($doc['turncity']=='1'){
            $content=$username."客户您好，南京平涛管理有限公司提醒您，您的银行贷款申请已结束，请您携带本人身份证原件到".$addre."平涛投资领取房产证及土地证。电话:".$tel;
          }
          elseif($doc['turncity']=='2'){
            $content=$username."客户您好，南京平涛管理有限公司提醒您，您的银行贷款申请已结束，请您携带本人身份证原件到江宁区新亭西路208号平涛投资领取房产证及土地证。电话:51198340联系人:储青霞。";
          }

      }
      $ruselt=sendSMS($phone,$content);

      if($ruselt=="success"){
      	if($type==1){
      	    $Audit = M("audit"); // 银行审核表通知更新
      	    $dataaudit['notice']=1;
            $Audit->where('doc_id='.$doc_id)->save($dataaudit);
        }
        elseif($type==2){
        	$Account = M("account"); // 公积金审核表通知更新
      	    $dataaudit['notice']=1;
            $Account->where('doc_id='.$doc_id)->save($dataaudit);
        }
        elseif($type==3){
        	$Mortgage = M("mortgage"); // 银行审核表通知更新
      	    $dataaudit['notice']=1;
            $Mortgage->where('doc_id='.$doc_id)->save($dataaudit);
        }
        elseif($type==4){
        	$Extend = M("extend"); // 银行审核表通知更新
      	    $dataaudit['agent_notice']=1;
            $Extend->where('doc_id='.$doc_id)->save($dataaudit);
        }
        elseif($type==5){
        	$Extend = M("extend"); // 银行审核表通知更新
      	    $dataaudit['client_notice']=1;
            $Extend->where('doc_id='.$doc_id)->save($dataaudit);
        }
            $this -> jsonReply('200' , '通知成功');
      }
      else{
            $this -> jsonReply('500' , '通知失败');
      }
    }
    public function sendfalsemessage(){
      $type=I('post.type');
      $phone=I('post.phone');
      $username=I('post.username');
      $doc_id=I('post.doc_id');
      $address = I('post.address');
      $falsecontent = I('post.falsecontent');
      $content="南京平涛管理有限公司提醒您，".$username."客户的案子，银行审核未通过，未通过原因：".$falsecontent;
      $ruselt=sendSMS($phone,$content);
      if($ruselt=="success"){
           $this -> jsonReply('200' , '通知成功');
      }
      else{
            $this -> jsonReply('500' , '通知失败');
      }
    }

    public function delDatas(){
        if(IS_POST){
          $point_id = D('Point') -> addPoint('Index/delDatas', '批量删除档案');//功能节点id
          $roleid = session('USER_AUTH.roleid');//权限角色id
          if( haveAblitity($point_id , $roleid) ){
            $id = I('post.id');
            if(count($id)==1){
              $del = D('Document') -> saveDoc($id[0],array('isdel'=>'1'));
            }
            if(count($id)>1){
              foreach($id as $k=>$v){
                $del = D('Document') -> saveDoc($v,array('isdel'=>'1'));
              }
            }
            if($del){
              $this -> jsonReply('200' , '删除成功');
            }
            else{
              $this -> jsonReply('500' , '删除失败');
            }
          }
          else{
            $this -> jsonReply('500' , '您没有权限操作此功能');
          }
        }
      }


       public function turnDatas(){
        if(IS_POST){
          $point_id = D('Point') -> addPoint('Index/turnDatas', '档案转入市区');//功能节点id
          $roleid = session('USER_AUTH.roleid');//权限角色id

          if( haveAblitity($point_id , $roleid) ){
            $id = I('post.id');
            if(count($id)==1){
                if(session('USER_AUTH.place')=='2'){
                    $del = D('Document') -> saveDoc($id[0],array('place'=>'1','turncity'=>'2'));
                }
                else{
                    $del = D('Document') -> saveDoc($id[0],array('place'=>'1','turncity'=>'1'));
                }

            }
            if(count($id)>1){
                if(session('USER_AUTH.place')=='2'){
                    foreach($id as $k=>$v){
                        $del = D('Document') -> saveDoc($v,array('place'=>'1','turncity'=>'2'));
                      }
                }
                else{
                    foreach($id as $k=>$v){
                        $del = D('Document') -> saveDoc($v,array('place'=>'1','turncity'=>'1'));
                      }
                }

            }
            if($del){
              $this -> jsonReply('200' , '转入成功');
            }
            else{
              $this -> jsonReply('500' , '档案已经在市区');
            }
          }
          else{
            $this -> jsonReply('500' , '您没有权限操作此功能');
          }
        }
      }

//退案
      public function tuiAn(){
        $doc_id = I('post.docid');
        $count = M('document')->where(array('doc_id'=>$doc_id))->find();
        $save = M('document')->where(array('doc_id'=>$doc_id))->save(array('state'=>'2','tuicount'=>($count['tuicount']+1)));
        $add = M('change_bank')->add(array('doc_id'=>$doc_id,'old_bank'=>$count['bank_id'],'old_loan'=>$count['loan'],'old_part'=>$count['part_bank_id'],'locktime'=>time()));

        if($save && $add){
          $this -> jsonReply('200' , '退案成功，待管理员解锁');
        }
        else{
          $this -> jsonReply('500' , '退案成功');
        }
      }

//解锁
      public function unlock(){
        $docid = I('get.docid');
        $doc = M('document')->where(array('doc_id'=>$docid,'isdel'=>'0'))->find();
        $this->assign('docid' , $doc['doc_id']);
        $this->assign('bank' , $doc['bank_id']);
        $banks = M('bank')-> where(array('pid'=>'0')) ->select();
        $this->assign('banks' , $banks);
        $this->display('Index/unlock');
      }

      public function unlockCheck(){
        $docid = I('post.doc_id');
        $doc = M('document')->where(array('doc_id'=>$docid))->find();
        $data['bank_id']=I('post.bank_id');
        $data['part_bank_id']=I('post.part_bank_id');
    		$data['loan'] = I('post.loan') ;
        $data['audit_time'] = empty(I('post.audit_time'))?'':timeStrop(I('post.audit_time')) ;
        $data['amount'] = I('post.total_amount');
        $data['state'] = '1';
        $sa1 = M('document')->where(array('doc_id'=>$docid))->save($data);

        $data2['bank_id'] = I('post.part_bank_id');
        $data2['plan_time'] = empty(I('post.audit_time'))?'':timeStrop(I('post.plan_time'));
        $data2['state'] = '0';
        $sa2 = M('audit') -> where(array('doc_id'=>$docid))->save($data2);

        $dataextend['account_amount']=I('post.account_amount');
        $dataextend['account_type']=I('post.account_type');
        $dataextend['business_amount']=I('post.business_amount');
        $dataextend['total_amount']=I('post.total_amount');
        $dataextend['loan_year']=I('post.loan_year');
        $dataextend['loan_remark']=I('post.loan_remark');
        $sa3 = M('extend') -> where(array('doc_id'=>$docid))->save($dataextend);

        $savedata = array(
          'new_bank'=>$data['bank_id'],
          'new_part'=>$data2['bank_id'],
          'unlocktime'=>time()
        );
        $sa4 = M('change_bank')->where(array('doc_id'=>$docid,'unlocktime'=>''))->save($savedata);
        if($sa4){
          $this->jsonReply('200','解锁成功');
        }
        else{
          $this->jsonReply('500','解锁失败');
        }

      }

      public function zhi(){
        $bank_id = I('post.id');
        $banks = M('bank') -> where(array('pid'=>$bank_id,'isdel'=>'0'))->select();
        exit(json_encode($banks));
      }


//超过30天未填写的列表
      public function warn(){
        $docs = M('document')->where(array('isdel'=>'0','state'=>array('neq','2')))->order('doc_id desc')->select();
        $arr = $this->wei($docs);
        $arr = $this->wei($docs);
        $count = count($arr);
        //$p = I('get.p');
        if($_GET['p']==''){
      	    $p=1;
      	}
      	else{
      	    $p = $_GET['p'];
      	}
      	$this->assign('p',$p);
        $offset = 15;
          $page=new \Think\Page($count,$offset);
          $show= $page->show();// 分页显示输出
          $this->assign('page',$show);
        $this->assign('count' , $count);

        $s = ( $p == 1) ?  '0'  : $offset * ($p -1);
        $e = $p? $offset * $p : $offset;
        foreach($arr as $kk=>$vv){
          if($kk<$e && $kk>=$s){
            $aa[]=$vv;
          }
        }
        $this->assign('list',$aa);
        $this->display();
      }

      private function wei($docs){
        foreach($docs as $k=>$v){
          $where['doc_id'] = $v['doc_id'];
          $budongchan = M('extend') -> where(array('doc_id'=>$v['doc_id'])) -> find();
          $account = M('account') -> where($where) ->find();
          $mortgage = M('mortgage') -> where($where)->find();
          if( (time()-$v['create_time'])>86400*30*3  && (empty($v['part_bank_id']) || $v['part_bank_id']=='0' || empty($v['audit_time'])) ){
            $list[] = array(
              'docid'    =>   $v['doc_id'],
              'signid'   =>   $v['sign_id'],
              'place'    =>  $v['place'],
              'buyname'  =>   $v['buy_name'],
              'creatime' =>   $v['create_time'],
              'bankid'   =>   $v['bank_id'],
              'part_bank'=>    $v['part_bank_id'],
              'audit_time' =>  $v['audit_time'],
              'type'     =>'银行送审时间'
            );
          }
          if(!empty($v['audit_time']) && ((time()-$v['audit_time'])>86400*3*30)){
            if(  empty($budongchan['certificate_time']) && $v['supervise']!='3'){
              $list[] = array(
                'docid'    =>   $v['doc_id'],
                'signid'   =>   $v['sign_id'],
                'place'    =>  $v['place'],
                'buyname'  =>   $v['buy_name'],
                'creatime' =>   $v['create_time'],
                'bankid'   =>   $v['bank_id'],
                'part_bank'=>    $v['part_bank_id'],
                'audit_time' =>  $v['audit_time'],
                'type'     =>'南房出件时间');
            }
            elseif(  empty($budongchan['estate_time']) ){
              $list[] = array(
                'docid'    =>   $v['doc_id'],
                'signid'   =>   $v['sign_id'],
                'place'    =>  $v['place'],
                'buyname'  =>   $v['buy_name'],
                'creatime' =>   $v['create_time'],
                'bankid'   =>   $v['bank_id'],
                'part_bank'=>    $v['part_bank_id'],
                'audit_time' =>  $v['audit_time'],
                'type'     =>'不动产证出件时间');
            }
            elseif( empty($account['real_time'])){
              $list[] = array(
                'docid'    =>   $v['doc_id'],
                'signid'   =>   $v['sign_id'],
                'place'    =>  $v['place'],
                'buyname'  =>   $v['buy_name'],
                'creatime' =>   $v['create_time'],
                'bankid'   =>   $v['bank_id'],
                'part_bank'=>    $v['part_bank_id'],
                'audit_time' =>  $v['audit_time'],
                'type'     =>'公积金实际审核时间');
            }
            elseif( empty($mortgage['real_time'])&& $v['supervise']=='3'){
              $list[] = array(
                'docid'    =>   $v['doc_id'],
                'signid'   =>   $v['sign_id'],
                'place'    =>  $v['place'],
                'buyname'  =>   $v['buy_name'],
                'creatime' =>   $v['create_time'],
                'bankid'   =>   $v['bank_id'],
                'part_bank'=>    $v['part_bank_id'],
                'audit_time' =>  $v['audit_time'],
                'type'     =>'抵押实际审核时间');
            }
            elseif(  empty($budongchan['else_time'])&& $v['supervise']=='3'){
              $list[] = array(
                'docid'    =>   $v['doc_id'],
                'signid'   =>   $v['sign_id'],
                'place'    =>  $v['place'],
                'buyname'  =>   $v['buy_name'],
                'creatime' =>   $v['create_time'],
                'bankid'   =>   $v['bank_id'],
                'part_bank'=>    $v['part_bank_id'],
                'audit_time' =>  $v['audit_time'],
                'type'     =>'出他证日期');
            }
            elseif( empty($budongchan['credit_real_time'])){
              $list[] = array(
                'docid'    =>   $v['doc_id'],
                'signid'   =>   $v['sign_id'],
                'place'    =>  $v['place'],
                'buyname'  =>   $v['buy_name'],
                'creatime' =>   $v['create_time'],
                'bankid'   =>   $v['bank_id'],
                'part_bank'=>    $v['part_bank_id'],
                'audit_time' =>  $v['audit_time'],
                'type'     =>'实际放款时间');
            }
          }
        }
        return $list;
      }



      public function tui(){
        $count = M('document')->where(array('tuicount'=>array('neq','0'),'isdel'=>'0'))->count();
        $this->assign('count',$count);
        $count1 = M('document')->where(array('tuicount'=>array('neq','0'),'isdel'=>'0','state'=>'2'))->count();
        $this->assign('count1',$count1);
        $count2 = M('document')->where(array('tuicount'=>array('neq','0'),'isdel'=>'0','state'=>array('neq','2')))->count();
        $this->assign('count2',$count2);
        if($_GET['p']==''){
      	    $p=1;
      	}
      	else{
      	    $p = $_GET['p'];
      	}
      	$this->assign('p',$p);
      	$n=15;
          $page=new \Think\Page($count,$n);
          $show= $page->show();// 分页显示输出
          $this->assign('page',$show);
          $arr = M('document')->where(array('tuicount'=>array('neq','0'),'isdel'=>'0'))->page($p.','.$n)->order('state desc')->select();
        foreach($arr as $k=>$v){
          $olds = array();
          $new = array();
          $time = array();
          $changes = M('change_bank')->where(array('doc_id'=>$v['doc_id']))->select();
          foreach($changes as $kk=>$vv){
            $olds[] = array(getBankname($vv['old_bank']),getBankname($vv['old_part']));
            $new[] = array(getBankname($vv['new_bank']),getBankname($vv['new_part']));
            $time[] = $vv['locktime'];
          }
          $list[]=array(
            'doc_id'=>$v['doc_id'],
            'state' =>$v['state'],
            'buy_name'=>$v['buy_name'],
            'creatime'=>$v['create_time'],
            'old'   =>$olds,
            'new'   => $new,
            'locktime' => $time
          );
        }
        // /p($list);
        $this->assign('list',$list);
        $this->display();
      }



      public function weijiesuo(){
        $where = array('tuicount'=>array('neq','0'),'isdel'=>'0','state'=>'2');
        $count = M('document')->where($where)->count();
        $this->assign('count',$count);
        if($_GET['p']==''){
      	    $p=1;
      	}
      	else{
      	    $p = $_GET['p'];
      	}
      	$this->assign('p',$p);
      	$n=15;
        $page=new \Think\Page($count,$n);
        $show= $page->show();// 分页显示输出
        $this->assign('page',$show);
        $arr = M('document')->where($where)->page($p.','.$n)->order('state desc')->select();
        foreach($arr as $k=>$v){
          $olds = array();
          $new = array();
          $time = array();
          $changes = M('change_bank')->where(array('doc_id'=>$v['doc_id']))->select();
          foreach($changes as $kk=>$vv){
            $olds[] = array(getBankname($vv['old_bank']),getBankname($vv['old_part']));
            $new[] = array(getBankname($vv['new_bank']),getBankname($vv['new_part']));
            $time[] = $vv['locktime'];
          }
          $list[]=array(
            'doc_id'=>$v['doc_id'],
            'state' =>$v['state'],
            'buy_name'=>$v['buy_name'],
            'creatime'=>$v['create_time'],
            'old'   =>$olds,
            'new'   => $new,
            'locktime' => $time
          );
        }
        // /p($list);
        $this->assign('list',$list);
        $this->display();
      }

      public function yijiesuo(){
        $where = array('tuicount'=>array('neq','0'),'isdel'=>'0','state'=>array('neq','2'));
        $count = M('document')->where($where)->count();
        $this->assign('count',$count);
        if($_GET['p']==''){
      	    $p=1;
      	}
      	else{
      	    $p = $_GET['p'];
      	}
      	$this->assign('p',$p);
      	$n=15;
        $page=new \Think\Page($count,$n);
        $show= $page->show();// 分页显示输出
        $this->assign('page',$show);
        $arr = M('document')->where($where)->page($p.','.$n)->order('state desc')->select();
        foreach($arr as $k=>$v){
          $olds = array();
          $new = array();
          $time = array();
          $changes = M('change_bank')->where(array('doc_id'=>$v['doc_id']))->select();
          foreach($changes as $kk=>$vv){
            $olds[] = array(getBankname($vv['old_bank']),getBankname($vv['old_part']));
            $new[] = array(getBankname($vv['new_bank']),getBankname($vv['new_part']));
            $time[] = $vv['locktime'];
          }
          $list[]=array(
            'doc_id'=>$v['doc_id'],
            'state' =>$v['state'],
            'buy_name'=>$v['buy_name'],
            'creatime'=>$v['create_time'],
            'old'   =>$olds,
            'new'   => $new,
            'locktime' => $time
          );
        }
        // /p($list);
        $this->assign('list',$list);
        $this->display();
      }

          //修改原因
      public function reasonedit(){
        $id = I('get.id');
        $reason = M('reason')->where(array('reason_id'=>$id))->find();
        $this->assign('reason',$reason);
        $this->display('Index/reason-edit');
      }

      public function editCheck(){
        $id = I('post.id');
        $content = I('post.reason');
        $save = M('reason') -> where(array('reason_id'=>$id))->save(array('content'=>$content));
        if($save){
          $this->jsonReply('200','修改成功');
        }
        else{
          $this->jsonReply('200','修改成功');
        }
      }
      //删除原因
      public function reasondel(){
        $id = I('post.id');
        $del = M('reason')->where(array('reason_id'=>$id))->delete();
        if($del){
          $this->jsonReply('200','删除成功');
        }
        else{
          $this->jsonReply('500','删除失败');
        }
      }


      public function adminEdit(){
      $doc_id= I('post.doc_id');
      $data['doc_type'] = I('post.doc_type');
      $data['need_day'] = I('post.needday');
      $data['buy_name'] =  I('post.buy_name');
      $data['sell_name'] = I('post.sell_name') ;
      $data['buy_card'] = I('post.buy_card') ;
      $data['spouse_card'] = I('post.spouse_card') ;
      $data['address'] = I('post.address') ;
      $data['supervise'] = I('post.supervise') ;
      $data['agent_id'] = I('post.agent') ;
      $data['bank_id'] = I('post.bank_id') ;
      $data['area'] = I('post.area');
      if(I('post.create_time')!=""){
          $data['create_time'] = strtotime(I('post.create_time')) ;
      }
      $data['sign_id'] = I('post.sign_id') ;
      $data['market_id'] = I('post.market_id') ;
      $data['amount'] = I('post.total_amount') ;
      $dataextend['agent_address'] = I('post.agentAddress');
      $dataextend['nature']=I('post.nature');
      $dataextend['buy_phone']=I('post.buy_phone');
      $dataextend['buy_tel']=I('post.buy_tel');
      $dataextend['buy_work']=I('post.buy_work');
      $dataextend['buy_work_tel']=I('post.buy_work_tel');
      $dataextend['buy_spouse_name']=I('post.buy_spouse_name');
      $dataextend['buy_spouse_tel']=I('post.buy_spouse_tel');
      $dataextend['sell_phone']=I('post.sell_phone');
      $dataextend['sell_tel']=I('post.sell_tel');
      $dataextend['account_amount']=I('post.account_amount');
      $dataextend['account_type']=I('post.account_type');
      $dataextend['business_amount']=I('post.business_amount');
      $dataextend['total_amount']=I('post.total_amount');
      $dataextend['loan_year']=I('post.loan_year');
      $dataextend['loan_remark']=I('post.loan_remark');
      $dataextend['house_area']=I('post.house_area');
      $dataextend['house_age']=I('post.house_age');
      $dataextend['house_total']=I('post.house_total');
      $dataextend['agent_name']=I('post.agent_name');
      $dataextend['agent_tel']=I('post.agent_tel');
      $dataextend['transfer_real_time']=strtotime(I('post.transfer_real_time'));
      $dataextend['certificate_time']=strtotime(I('post.certificate_time'));
      $dataaudit['bank_id']=I('post.part_bank_id') ;
      $data['part_bank_id']=I('post.part_bank_id') ;
      if(I('post.part_bank_id')){
                          $data['state']=1;
                        }
      else{
      $data[‘state’]=0;
      }
      $data['loan'] = I('post.loan') ;
      $dataaudit['plan_time']=strtotime(I('post.plan_time'));
      if(I('post.reason')==null){//ttmr改了，判断出错
            	$dataaudit['state']=1;
            }
      else{
          $dataaudit['state']='0';
          $dataaudit['reason']=I('post.reason') ;
          if($user_auth['roleid']==2||$user_auth['roleid']==6||$user_auth['roleid']==11){
            $data['ignore_sign'] = 'n';
          }
          elseif($user_auth['roleid']==1){
            $data['ignore_market'] = 'n';
          }

      }
      $data['audit_time'] = strtotime(I('post.audit_time')) ;
      $data['audit_finish'] = strtotime(I('post.audit_finish')) ;
      if(I('post.reason')!=null){
                  	$auditreason['doc_id'] = $doc_id ;
                  	$auditreason['type'] = 1 ;
                  	$auditreason['creat_time'] = time() ;
                  	$auditreason['content'] = I('post.reason') ;
                    $auditreason['bank_id'] = $state['part_bank_id'];
                  	$Reason = M("reason"); // 银行审核未通过原因添加
                      $Reason->add($auditreason);

                  }
      $dataextend['transfer_real_time']=strtotime(I('post.transfer_real_time'));
      $dataextend['certificate_time']=strtotime(I('post.certificate_time'));
      $dataextend['estate_time']=strtotime(I('post.estate_time'));
      $dataextend['commission']=I('post.commission');
      $dataextend['estate_remark']=I('post.estate_remark');
      $dataaccount['audit_time']=strtotime(I('post.account_audit_time'));
      $dataaccount['plan_time']=strtotime(I('post.account_plan_time'));
      $dataaccount['real_time']=strtotime(I('post.account_real_time'));
      $dataaccount['reason']=I('post.account_reason');
      if(empty(I('post.account_reason'))){
          $dataaccount['state']='1';
      }
      else{
          $dataaccount['state']='0';
      }
      if(I('post.account_reason')!=null){
      	$auditreason['doc_id'] = $doc_id ;
      	$auditreason['type'] = 2 ;
      	$auditreason['creat_time'] = time() ;
      	$auditreason['content'] = I('post.account_reason') ;
      	$Reason = M("reason"); // 公积金审核未通过原因添加
          $Reason->add($auditreason);
      }
      $dataextend['transfer_time']=strtotime(I('post.transfer_time'));
      $datamortgage['transfer_time']=strtotime(I('post.mortgage_transfer_time'));
      $datamortgage['transfer_time']=strtotime(I('post.mortgage_transfer_time'));
      $datamortgage['get_time']=strtotime(I('post.mortgage_get_time'));
      if(I('post.mortgage_reason')){
              $datamortgage['reason']=I('post.mortgage_reason');
              $auditreason['doc_id'] = $doc_id ;
              $auditreason['type'] = 3 ;
              $auditreason['creat_time'] = time() ;
              $auditreason['content'] = I('post.mortgage_reason') ;
              $Reason = M("reason"); // 公积金审核未通过原因添加
                $Reason->add($auditreason);
            }
            else{
              $datamortgage['reason']='';
            }
      $datamortgage['plan_time']=strtotime(I('post.mortgage_plan_time'));
      $datamortgage['real_time']=strtotime(I('post.mortgage_real_time'));
      if(I('post.mortgage_real_time')!=""){
      $datamortgage['state']=1;
      }
      else{
      $datamortgage['state']=0;

      }
      $dataextend['else_time']=strtotime(I('post.else_time'));
      $dataextend['else_plan_time']=strtotime(I('post.else_plan_time'));
      $data['credit_real_time'] = strtotime(I('post.credit_real_time')) ;
      $data['credit_time']=strtotime(I('post.credit_time'));$dataextend['credit_time']=strtotime(I('post.credit_time'));
      $dataextend['credit_real_money']=I('post.credit_real_money');
      $dataextend['credit_remark']=I('post.credit_remark');
      $datacharge['charge_time']=I('post.charge_time');
      if(I('post.certificate2_time')!=''){
        $datacharge['certificate_time']=strtotime(I('post.certificate2_time'));
        $datacharge['certificate']=1;
        $datacharge['put_userid']=$_SESSION['userid'];
      }
      $data['latent'] = I('post.latent') ;
      $data['approver'] = $user_auth['userid'] ;
      $Document = M("document"); // 档案表更新
        $document=$Document->where('doc_id='.$doc_id)->save($data);

        $Audit = M("audit"); // 银行审核表更新
        $audit=$Audit->where('doc_id='.$doc_id)->save($dataaudit);

        $Account = M("account"); // 公积金核表更新
        $account=$Account->where('doc_id='.$doc_id)->save($dataaccount);

        $Mortgage = M("mortgage"); // 抵押表更新
        $mortgage = $Mortgage->where('doc_id='.$doc_id)->save($datamortgage);

        $Extend = M("extend"); // 信息扩展表更新
        $extend=$Extend->where('doc_id='.$doc_id)->save($dataextend);

        $Charge = M("charge"); // 收费发证表更新
        $charge=$Charge->where('doc_id='.$doc_id)->save($datacharge);

        if($charge || $mortgage || $account || $audit || $document || $extend){
                $this -> jsonReply('200' , '审核成功');
              }
        else{
            $this -> jsonReply('200' , '修改成功');
        }
    }


    public function huanyuan(){
      $id = I('post.id');
      $save = M('document')->where(array('doc_id'=>$id))->save(array('doc_type'=>'','need_day'=>''));
      if($save){
        $this -> jsonReply('200' , '还原成功');
      }
      else{
        $this -> jsonReply('500' , '还原失败');
      }
    }









}
